<?php
/**
 * @copyright Copyright (c) 2021 四合云创工作室
 * @license https://opensource.org/licenses/GPL-3.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\home\controller;

use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class Log extends BaseController
{
    //管理员操作日志
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['u.name|a.param_id|a.uid', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['action'])) {
                $where[] = ['a.action','=',$param['action']];
            }
            if($this->eid > 0) {
                $where[] = ['a.eid','=',$this->eid];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            if($this->eid > 0) {
                $list = DB::name('AdminLog')
                    ->field("a.*,u.name")
                    ->alias('a')
                    ->join('Admin u', 'a.uid = u.id')
                    ->order('a.create_time desc')
                    ->where($where)
                    ->paginate($rows, false, ['query' => $param])
                    ->each(function($item, $key){
                        $ename = '系统';
                        if($item['eid'] > 0){
                            $ename = Db::name('Enterprise')->where('id',$item['eid'])->value('title');
                        }
                        $item['content'] = '【'.$ename.'】'.$item['name']. $item['action'] . '了' . $item['subject'];
                        $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                        $item['param'] = !empty(json_decode($item['param'])) ? $item['param']: '';
//                        $param_array = json_decode($item['param'], true);
//                        if(is_array($param_array)){
//                            $param_value = [];
//                            foreach ($param_array as $key => $value) {
//                                if (is_array($value)) {
////                                    $value = implode(',', $value);
//                                    $value = json_encode($value);
//                                }
//                                $param_value[] = $key . ':' . $value;
//                            }
//                            $item['param'] = implode(' & ',$param_value);
//                        }
//                        else{
//                            $item['param'] = $param_array;
//                        }
                        return $item;
                    });
            }else{
                $list = DB::name('AdminLog')
                    ->field("a.*,u.name")
                    ->alias('a')
                    ->join('Admin u', 'a.uid = u.id')
                    ->order('a.create_time desc')
                    ->where($where)
                    ->paginate($rows, false, ['query' => $param])
                    ->each(function($item, $key){

                        $ename = '系统';
                        if($item['eid'] > 0){
                            $ename = Db::name('Enterprise')->where('id',$item['eid'])->value('title');
                        }
                        $item['content'] = '【'.$ename.'】'.$item['name']. $item['action'] . '了' . $item['subject'];
                        $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                        $item['param'] = !empty(json_decode($item['param'])) ? $item['param']: '';
//                        $param_array = json_decode($item['param'], true);

//                        if(is_array($param_array) && !empty($param_array)){
//                            $param_value = [];
//                            foreach ($param_array as $key => $value) {
//                                if (is_array($value) && !empty($value)) {
////                                    $value = implode(',', $value);
//                                    $value = json_encode($value);
//
//                                }
//                                $param_value[] = $key . ':' . $value;
//                            }
//                            $item['param'] = implode(' & ',$param_value);
//                        }
//                        else{
//                            $item['param'] = $param_array;
//                        }
                        return $item;
                    });
            }
            return table_assign(0, '', $list);
        } else {
			$type_action = get_config('log.type_action');
			View::assign('type_action', $type_action);
            return view();
        }
    }
}

